package org.popupcommunication.controller;


import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.popupcommunication.dao.GroupDAO;
import org.popupcommunication.model.AgendaGroupe;
import org.popupcommunication.model.Group;
import org.popupcommunication.model.UploadedFile;
import org.popupcommunication.service.AgendaGroupeManager;
import org.popupcommunication.service.GroupManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



@Controller
@RequestMapping("admin/agenda")
public class AdminAgendaController {

	@Autowired
	AgendaGroupeManager agendaGroupeService;
	@Autowired
	GroupManager groupService;
	@Autowired
	GroupDAO groupRepository;

	List<Group> lstGrp;
	Map<Integer, Group> groupeCache;
	UploadedFile ufile;

	private static final String form_ajout = "agendaGroupe/ajoutEvenement";
	private static final String db_ajout_confirm = "agendaGroupe/confirmationEvenement";

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.setAllowedFields(new String[] { "dateExpiration", "description",
				"titre", "groupe","img","dateDebut","fileupload" });

		binder.registerCustomEditor(Group.class, "groupe",
				new PropertyEditorSupport() {
					@Override
					public void setAsText(String id) {
						Group gg = groupRepository.findById(Integer.valueOf(id));
						setValue(gg);
					}
				});

		/*
		 * 
		 * binder.registerCustomEditor(Set.class,"groupe", new
		 * CustomCollectionEditor(Set.class){ protected Object
		 * convertElement(Object element){ if (element instanceof String) {
		 * Group g = groupeCache.get(Integer.parseInt(element.toString()));
		 * 
		 * return g; } return null; } });
		 */
	}

	@ModelAttribute("lstGroupes")
	public List<Group> getGroups() {
		return groupService.getAllGroups();
	}
	
	
	
	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String showForm(Model model) {
		lstGrp = groupService.getAllGroups();
		groupeCache = new HashMap<Integer, Group>();
		for (Group g : lstGrp) {
			groupeCache.put((Integer) g.getIdGroup(), g);
		}
		model.addAttribute("lstGroupes", lstGrp);
		model.addAttribute("agenda", new AgendaGroupe());
		return form_ajout;
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String postForm(@ModelAttribute("agenda") @Valid AgendaGroupe form,
			BindingResult result) {

		// ajout database
		agendaGroupeService.registerAgendaGroupe(form, result);

		return (result.hasErrors() ? form_ajout : db_ajout_confirm);
	}


}
